package webalbum.google;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Link;
import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.media.mediarss.MediaGroup;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import webalbum.WebAlbum;
import webalbum.WebAlbumRetriever;

public final class GoogleWebAlbumRetriever implements WebAlbumRetriever
{
    private static GoogleWebAlbumRetriever instance;
    static
    {
        instance = new GoogleWebAlbumRetriever();
    }
    private GoogleWebAlbumRetriever(){}
    public synchronized static WebAlbumRetriever getInstance()
    {
        return instance;
    }
    
    public List<WebAlbum> getWebAlbums(URI uri) throws Exception
    {
        PicasawebService myService = new PicasawebService("WebAlbumRetriever");
        URL albumsUrl = new URL(uri.toURL().toString()+"?kind=album");
        List<WebAlbum> albums = new ArrayList<WebAlbum>();
        // Send the request for the user's albums.
        UserFeed myUserFeed = myService.getFeed(albumsUrl, UserFeed.class);

        for (AlbumEntry ae : myUserFeed.getAlbumEntries())
        {
            URL albumAndPhotosUrl = new URL(uri.toURL().toString()+"/album/"+ae.getName()+"?kind=photo");

            // Send the request for the album's photos.
            AlbumFeed albumFeed = myService.getFeed(albumAndPhotosUrl, AlbumFeed.class);
            albums.add(new GoogleWebAlbum(albumFeed));
        }        
        return albums;
    }
    
    public static void main(String arg[]) throws Exception
    {
        String baseFeedURL = "http://picasaweb.google.com/data/feed/api/user/zac.heismann";
        String baseEntryURL = "http://picasaweb.google.com/data/entry/api/user/zac.heismann";
        URL albumsUrl = new URL(baseFeedURL+"?kind=album");
        PicasawebService myService = new PicasawebService("WebAlbumRetriever");

        // Send the request for the user's albums.
        UserFeed myUserFeed = myService.getFeed(albumsUrl, UserFeed.class);

        for (AlbumEntry ae : myUserFeed.getAlbumEntries())
        {
            System.out.println(ae.getName() + "\t"+ae.getId());
            URL albumAndPhotosUrl = new URL(baseFeedURL+"/album/"+ae.getName()+"?kind=photo");

            // Send the request for the album's photos.
            AlbumFeed myAlbumFeed = myService.getFeed(albumAndPhotosUrl, AlbumFeed.class);
            // Print the title of the returned feed:
            System.out.println(myAlbumFeed.getTitle().getPlainText());
            System.out.println("size: " + myAlbumFeed.getPhotoEntries().size());
            for (PhotoEntry pe : myAlbumFeed.getPhotoEntries())
            {
                URL photoUrl = new URL(pe.getId());
//                PhotoFeed photoFeed = myService.getFeed(photoUrl, PhotoFeed.class);
//                photoFeed.getService()
//                System.out.println("photoFeed.getHtmlLink(): " + photoFeed.getHtmlLink());
                
/*                for (Link link : photoFeed.getLinks())
                {
                    System.out.println("href: " + link.getHref());
                }
 */
               /* System.out.println(pe.getGphotoId()
                        + "\n\t" + pe.getAlbumId()
                        + "\n\t" + pe.getChecksum()
                        + "\n\t" + pe.getClient()
                        + "\n\t" + pe.getId()
                        + "\n\t" + pe.getVersionId()
                        + "\n\t" + pe.getCommentCount()
                        + "\n\t" + pe.getCommentsEnabled()
                        + "\n\t" + pe.getExifTags()
                        + "\n\t" + pe.getGeoLocation()
                        + "\n\t" + pe.getHeight()
                        + "\n\t" + pe.getKeywords()
                        + "\n\t" + pe.getPosition()
                        + "\n\t" + pe.getRotation()
                        + "\n\t" + pe.getSize()
                        + "\n\t" + pe.getStreamIds()
                        + "\n\t" + pe.getTimestamp()
                        + "\n\t" + pe.getVersion()
                        + "\n\t" + pe.getWidth()
                        );
                
                MediaSource ms = pe.getMediaSource();
                if (ms != null)
                {
                    System.out.println("  " + ms.getName());
                }
                */
                System.out.println(pe.getId()
                       + "\n\t" + pe.getSize()
                        );
                MediaGroup mg = pe.getExtension(MediaGroup.class);
                if (mg != null)
                {
                    List<MediaContent> mediaContents = mg.getContents();
                    for (MediaContent mc : mediaContents)
                    {
                        System.out.println("\tmc.getUrl(): " + mc.getUrl());
                    }
                }
                for (Link link : pe.getLinks())
                {
                System.out.println(
                       "\tLink: " + link.getType()
                     + "\n\tLink: " + link.getTitle()
                     + "\n\tLink: " + link.getHref()
                     + "\n\tLink: " + link.getRel()
                    );
                }
            }
        }
        // Print the title of the returned feed:
        System.out.println(myUserFeed.getTitle().getPlainText());
    }
}


